<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>

          <a-col  :span="24">
            <a-form-item label="计划名称" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-input v-decorator="['name']" placeholder="请输入计划名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="计划开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                v-decorator="[ 'startTime', validatorRules.startTime]"
                :trigger-change="true"
                date-format="YYYY-MM-DD"
                placeholder="请选择开始日期" >
              </j-date>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="计划结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                v-decorator="[ 'endTime', validatorRules.endTime]"
                :trigger-change="true"

                date-format="YYYY-MM-DD"
                style="width:100%"
                placeholder="请选择计划结束日期" >
              </j-date>

            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="工作计划" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['content']" rows="4" placeholder="请输入工作计划"/>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="完成情况" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="planCompleteTable.loading"
            :columns="planCompleteTable.columns"
            :dataSource="planCompleteTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>

      </a-tabs>

    </a-spin>
  </j-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import moment from "moment"
  import JDate from '@/components/jeecg/JDate'
  export default {
    name: 'PlanWorkplanModal',
    mixins: [JEditableTableMixin],
    components: {
      JDate,moment
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          endTime:{rules:[{ required: true, message: '请选择计划结束时间!'} ,{validator: this.endTimeValidate}]},
          startTime:{rules:[{required: true, message: '请选择计划开始时间!'},{validator: this.startTimeValidate}]},
        },
        startTimeValidate(rule,value,callback){
          let endTime = this.form.getFieldValue("endTime")
          if(!value || !endTime){
            callback()
          }else if(moment(value).isBefore(endTime)){
            callback()
          }else{
            callback("开始时间需小于结束时间")
          }
        },
        endTimeValidate(rule,value,callback){
          let startTime = this.form.getFieldValue("startTime")
          if(!value || !startTime){
            callback()
          }else if(moment(startTime).isBefore(value)){
            callback()
          }else{
            callback("结束时间需大于开始时间")
          }
        },

        refKeys: ['planComplete', ],
        tableKeys:['planComplete', ],
        activeKey: 'planComplete',
        // 完成情况
        planCompleteTable: {
          loading: false,
          dataSource: [],
          columns: [
            /*{
              title: '计划上报人',
              key: 'userId',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '上报日期',
              key: 'workDate',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },*/
            {
              title: '完成情况描述',
              key: 'description',
              type: FormTypes.input,
              width:"800px",
              placeholder: '请输入${title}',
              defaultValue: '',
            }
            /*,
            {
              title: '工作计划',
              key: 'workplanId',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },*/
          ]
        },
        url: {
          add: "/luban/planWorkplan/add",
          edit: "/luban/planWorkplan/edit",
          planComplete: {
            list: '/luban/planWorkplan/queryPlanCompleteByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'name','startTime','endTime','content','userId','createBy','state')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.planComplete.list, params, this.planCompleteTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          planCompleteList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'name','startTime','endTime','content','userId','createBy','state'))
     },

    }
  }
</script>

<style scoped>
</style>